package com.ruoyi.merchant.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.merchant.domain.*;
import com.ruoyi.merchant.entity.H002Model;
import com.ruoyi.merchant.service.HealthCheckService;
import com.ruoyi.merchant.service.IBaseDeviceInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName HealthCheckController
 * @Description TODO
 * @Author chenxin.ju
 * @Date 2020-07-15 17:06
 * @ProjectName juservicecloud
 * @Version 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/health")
public class HealthCheckController extends BaseController {

    @Autowired
    private HealthCheckService healthCheckService;

    /**
     * 获取计步数据 24小时、7 周、30 月、120 季、360 年
     * @param customerId
     * @return
     */
    @GetMapping("/step")
    public AjaxResult getStepData(String customerId, int day){
        List<C012DataBuShu> buShuList = new ArrayList<>();
        try{
            buShuList = healthCheckService.getStepData(customerId,day);
        }catch (Exception e){
            log.error("getStepData",e);
            return error("获取计步数据失败");
        }

        return success(buShuList);
    }

    /**
     * 获取睡眠数据 24小时、7 周、30 月、120 季、360 年
     * @param imei
     * @return
     */
    @GetMapping("/sleep")
    public AjaxResult getSleepData(String customerId,int day){
        List<C012DataSleep> sleepList = new ArrayList<>();
        try{
            sleepList = healthCheckService.getSleepData(customerId,day);
        }catch (Exception e){
            log.error("getSleepData",e);
            return error("获取睡眠数据失败");
        }

        return success(sleepList);
    }


    /**
     * 获取体温数据 24小时、7 周、30 月、120 季、360 年
     * @param customerId
     * @return
     */
    @GetMapping("/temperature")
    public AjaxResult getTemperature(String customerId,int day){
        List<TemperatureData> temperatureDataList =  new ArrayList<>();
        try{
            temperatureDataList = healthCheckService.getTemperature(customerId,day);
        }catch (Exception e){
            log.error("getSleepData",e);
            return error("获取体温数据失败");
        }

        return success(temperatureDataList);
    }


    /**
     * 获取呼吸数据 24小时、7 周、30 月、120 季、360 年
     * @param imei
     * @return
     */
    @GetMapping("/respiratoryRate")
    public AjaxResult getRespiratoryRate(String customerId,int day){
        List<RespiratoryRateData> respiratoryRateDataList = new ArrayList<>();
        try{
            respiratoryRateDataList = healthCheckService.getRespiratoryRate(customerId,day);
        }catch (Exception e){
            log.error("getSleepData",e);
            return error("获取呼吸数据失败");
        }

        return success(respiratoryRateDataList);
    }


    /**
     * 获取血糖数据 24小时、7 周、30 月、120 季、360 年
     * @param imei
     * @return
     */
    @GetMapping("/bloodSugar")
    public AjaxResult getBloodSugar(String customerId,int day){
        List<BloodSugarData> bloodSugarDataList = new ArrayList<>();
        try{
            bloodSugarDataList = healthCheckService.getBloodSugarData(customerId,day);
        }catch (Exception e){
            log.error("getBloodSugar",e);
            return error("获取血糖数据失败");
        }

        return success(bloodSugarDataList);
    }

    /**
     * 获取血脂数据 24小时、7 周、30 月、120 季、360 年
     * @param imei
     * @return
     */
    @GetMapping("/cholesterol")
    public AjaxResult getCholesterol(String customerId,int day){
        List<CholesterolData> cholesterolDataList =  new ArrayList<>();
        try{
            cholesterolDataList = healthCheckService.getCholesterolData(customerId,day);
        }catch (Exception e){
            log.error("getBloodOxygen",e);
            return error("获取血脂数据失败");
        }

        return success(cholesterolDataList);
    }


    /**
     * 获取尿酸数据 24小时、7 周、30 月、120 季、360 年
     * @param imei
     * @return
     */
    @GetMapping("/uricAcid")
    public AjaxResult getUricAcidData(String customerId,int day){
        List<UricAcidData> uricAcidDataList = new ArrayList<>();
        try{
            uricAcidDataList = healthCheckService.getUricAcidData(customerId,day);
        }catch (Exception e){
            log.error("getBloodOxygen",e);
            return error("获取尿酸数据失败");
        }

        return success(uricAcidDataList);
    }


    /**
     * 获取血氧数据 24小时、7 周、30 月、120 季、360 年
     * @param imei
     * @return
     */
    @GetMapping("/bloodOxygen")
    public AjaxResult getBloodOxygen(String customerId,int day){
        List<BloodOxygenData> bloodOxygenData =  new ArrayList<>();
        try{
            bloodOxygenData = healthCheckService.getBloodOxygenData(customerId,day);
        }catch (Exception e){
            log.error("getBloodOxygen",e);
            return error("获取血氧数据失败");
        }

        return success(bloodOxygenData);
    }

    /**
     * 获取心率数据 24小时、7 周、30 月、120 季、360 年
     * @param imei
     * @return
     */
    @GetMapping("/bat")
    public AjaxResult getBatData(String customerId,int day){
        List<C012DataBat> c012DataBatList =  new ArrayList<>();
        try{
            c012DataBatList = healthCheckService.getBatData(customerId,day);
        }catch (Exception e){
            log.error("getBatData",e);
            return error("获取心率数据失败");
        }

        return success(c012DataBatList);
    }

    /**
     * 获取血压数据 24小时、7 周、30 月、120 季、360 年
     * @param imei
     * @return
     */
    @GetMapping("/bp")
    public AjaxResult getBpData(String customerId,int day){
        List<C010DataBP> c010DataBPList = new ArrayList<>();
        try{
            c010DataBPList = healthCheckService.getBpData(customerId,day);
        }catch (Exception e){
            log.error("getBpData",e);
            return error("获取血压数据失败");
        }

        return success(c010DataBPList);
    }

    @Autowired
    private IBaseDeviceInfoService baseDeviceInfoService;

    /**
     * 获取实时健康数据
     * @param customerId
     * @return
     */
    @GetMapping("/InstantHealth")
    @ResponseBody
    public  AjaxResult InstantHealthDetection(String customerId){
        H002Model h002Model = null;
        String imei = "";
        try{
//            BaseDeviceInfo param = new BaseDeviceInfo();
//            param.setCustomerId(customerId);
//            param.setDel(0L);
//            param.setStatus(0L);
//            List<BaseDeviceInfo> baseDeviceInfoList = baseDeviceInfoService.selectBaseDeviceInfoList(param);
//            if (CollectionUtils.isEmpty(baseDeviceInfoList)){
//                log.error("该客户未绑定设备或者设备已冻结{}", JSON.toJSONString(baseDeviceInfoList));
//                return   error("该客户未绑定设备或者设备已冻结");
//            }
//            BaseDeviceInfo baseDeviceInfo = baseDeviceInfoList.get(0);
//            log.info("客户信息：{}", JSON.toJSONString(baseDeviceInfo));
            if (StringUtils.isEmpty(customerId)){
                return   error("请传参数客户id！！");
            }

            h002Model = healthCheckService.InstantHealthDetection(customerId);
            if (h002Model == null ){
                log.error("获取实时健康数据失败{}", JSON.toJSONString(h002Model));
                return   error("实时健康数据不存在，请调试设备确认数据已上传！！");
            }
        }catch (Exception e){
            log.error("InstantHealthDetection",e);
            return   error("获取实时健康数据失败");
        }
        return success(h002Model);
    }


}
